<?php
namespace Home\Controller;
use Think\Controller;

class AnalyseController extends Controller{
	public function index(){
		//初始化数组data
		$data = array();
		
		//对基本电影类型进行分析
		$res = M('FilmDetail')->alias('fd')->field('avg(fd.film_score) as avg_score,avg(fd.film_vote) as avg_vote,fi.film_type')->join('left join ot_film_info as fi on fi.id = fd.film_id')->group('fi.film_type')->select();
		$TYPE = C('FILM_TYPE');
		
		foreach($res as $key => $val){
			$data['rank_type'][$key]['type_name'] = $TYPE[$val['film_type']];
			$data['rank_type'][$key]['avg_score'] = $val['avg_score'];
			$data['rank_type'][$key]['avg_vote'] = $val['avg_vote'];
			$data['rank_type'][$key]['rank_score'] = (round($val['avg_score']*10000)*0.85) + (round($val['avg_vote'])*0.15); 
		}
		//dump($data['rank_type']);
		
		//对所有电影类型进行分析
		$list = M('FilmDetail')->field('avg(film_score) as avg_score,film_type,avg(film_vote) as avg_vote')->group('film_type')->order('avg(film_score) desc')->select();
		
		foreach($list as $key=>$val){
			if($val['film_type'] != "" && $val['film_type'] != "Reality-TV" ){
				$data['rank_1'][$key]['avg_score'] = round($val['avg_score'],2)*10000;
				$data['rank_1'][$key]['film_type'] = $val['film_type'];
				$data['rank_1'][$key]['avg_vote'] = round($val['avg_vote']);
			}
		}

		//针对主要电影类型进行分析
		$film_type = array("纪录片","剧情","动画","战争","喜剧","动作","奇幻","犯罪","爱情","历史","悬疑","情色");
		
		foreach($list as $key=>$val){
			if(in_array($val['film_type'], $film_type)){
				if($val['film_type'] != "" && $val['film_type'] != "Reality-TV" ){
				$data['rank_2'][$key]['avg_score'] = round($val['avg_score'],2)*10000;
				$data['rank_2'][$key]['film_type'] = $val['film_type'];
				$data['rank_2'][$key]['avg_vote'] = round($val['avg_vote']);
				$data['rank_2'][$key]['jiaquan'] = (round($val['avg_score'],2)*10000*0.85) + (round($val['avg_vote'])*0.15);
			}
			}

		}
		
		//通过数据分析获取最受观众喜欢的电影
		$time = time();
		$list_1 = M('FilmDetail')->field('fi.film_name_real,fi.id,fd.film_score,fd.film_vote,fd.film_type')->alias('fd')->join('ot_film_info as fi on fi.id = fd.film_id')->group('fi.film_name_real')->order('fd.film_score desc')->select();
		$film_name = array("宝莱坞劲敌","宝莱坞机器人之恋","宝莱坞大玩家","飞","子弹","海上传奇","再见，我的皇后","阿凡达加长版","阿凡达/化身","飞屋环游记/天外奇迹/冲天救兵","切除/开膛少女的异想世界","星际奇兵/星际之门","初恋","V字特工队","报复/仇杀/怪客","仇杀病房/仇杀病室","爱在罗马/午夜罗马","无法触碰/触不可及","失恋33天/非常完美2","三个白痴/三傻大闹宝莱坞","禁闭岛/孤岛疑云","永恒岛/梦幻岛","西雅图之战","骄傲大联盟","危险人物/起尾注","危险人物","三分钟先生","合伙人","海猿4：勇敢的心","蝙蝠侠：黑暗骑士归来（下）","蝙蝠侠：黑暗骑士归来(上)","真的恋爱了","志明与春娇/谈恋爱","魂断疯人院","致命性游戏","时尚女魔头","驯龙高手/驯龙记","射雕英雄传之东成西就","唐伯虎点秋香2","唐伯虎点秋香2","哈利・波特与死亡圣器(下)","飞扬的青春","没有青春的青春","我们毕业的夏天","我们的家庭婚礼","忘不了","欢迎来到利雷家","涉外大酒店","欢迎来到庞奇","2012末日预言","探索频道解码世界末日2012","末日预言","2012世界末日/2012末日预言","恐怖游轮/迷失三角洲","学校的地下网站","社交网络/社群网战","被破坏的男子","被抢劫的乐趣","我和我的女友","我是你的野蛮女友","野蛮女人","我的野蛮女友2","我的僵尸女友","我的播音系女友","前任/旧爱喜欢你","里约大冒险2","里约大冒险/奇鹦嘉年华","海湾峡谷/海豚湾","夏洛特的网","未来战警","嗜血破晓/黎明时分","春娇与志明","霍比特人1：意外旅程","潜艇总动员3","第九突击队/第九连","玩具总动员之惊魂夜","皇家师姐之皇家战士","控制的极限/极限控制","蝙蝠侠：第一年","混蛋");
		$i = 1;
		foreach($list_1 as $key => $val){
			if(!in_array($val['film_name_real'], $film_name)){
				$data['rank_3'][$key]['id'] = $val['id'];
				$data['rank_3'][$key]['film_name_real'] = $val['film_name_real'];
				$data['rank_3'][$key]['rank_score'] = round(((round($val['film_score']*10000)*0.82) + (round($val['film_vote'])*0.18))/(2000),2);
//				if($val['film_vote'] < 10000 && $val['film_vote'] > 0){
//					$data['rank_3'][$key]['rank_score'] = round(((round($val['film_score']*10000)*0.83) + (round($val['film_vote'])*0.17))/(2000),2);
//				}elseif($val['film_vote'] < 100000 && $val['film_cote'] >=10000){
//					$data['rank_3'][$key]['rank_score'] = round(((round($val['film_score']*10000)*0.73) + (round($val['film_vote'])*0.27))/(2000),2);
//				}elseif($val['film_vote'] <200000  && $val['film_cote'] >=100000){
//					$data['rank_3'][$key]['rank_score'] = round(((round($val['film_score']*10000)*0.63) + (round($val['film_vote'])*0.37))/(2000),2);
//				}elseif($val['film_vote'] < 300000 && $val['film_cote'] >=200000){
//					$data['rank_3'][$key]['rank_score'] = round(((round($val['film_score']*10000)*0.53) + (round($val['film_vote'])*0.47))/(2000),2);
//				}elseif($val['film_vote'] < 400000 && $val['film_cote'] >=300000 ){
//					$data['rank_3'][$key]['rank_score'] = round(((round($val['film_score']*10000)*0.33) + (round($val['film_vote'])*0.57))/(2000),2);
//				}elseif($val['film_vote'] < 600000 && $val['film_cote'] >=400000){
//					$data['rank_3'][$key]['rank_score'] = round(((round($val['film_score']*10000)*0.23) + (round($val['film_vote'])*0.77))/(2000),2);
//				}elseif($val['film_vote'] < 1000000 && $val['film_cote'] >=600000){
//					$data['rank_3'][$key]['rank_score'] = round(((round($val['film_score']*10000)*0.18) + (round($val['film_vote'])*0.82))/(2000),2);
//				}else{
//					$data['rank_3'][$key]['rank_score'] = round(((round($val['film_score']*10000)*0.10) + (round($val['film_vote'])*0.9))/(2000),2);
//				}
				$data['rank_3'][$key]['film_score'] = $val['film_score'];
				$data['rank_3'][$key]['film_vote'] = $val['film_vote'];
				$data['rank_3'][$key]['film_type'] = $val['film_type'];
				
			}
			}
			//echo "筛选用时:".(time()-$time)."s<br>";
		
		$tmp = array();
		//冒泡排序对电影进行排序
		$time1 = time();
		for($i = 0;$i < count($data['rank_3']);$i++){
			for($j = count($data['rank_3']) - 1;$j > $i;$j--){
				if($data['rank_3'][$j]['rank_score'] > $data['rank_3'][$j-1]['rank_score']){
					$tmp = $data['rank_3'][$j];
					$data['rank_3'][$j] = $data['rank_3'][$j-1];
					$data['rank_3'][$j-1] = $tmp;
					
				}
			}
			$data['rank_3'][$i]['ranking'] = $i+1;
		}
		
		//echo "冒泡排序".count($data['rank_3'])."条数据用时:".(time()-$time1)."s\n";
		//dump($data['rank_3']);
		$this->assign("data",$data);
		$this->display();
	}

}	
?>